package com.cn.qtms.common.codename;//类注释设置模板

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

/**
 * @Name:DataSourceItem
 * @Description:
 * @Author: zhangyang
 * @Date: 2019-08-08 23:24
 * 所有的下拉框数据工具类
 **/
@Data
@NoArgsConstructor
public class CodeNameItem implements Serializable {
    private String name;
    private String label;
    private Object value;
    private Object desc;

    public CodeNameItem(String label, Object value, Object desc) {
        this.label = label;
        this.value = value;
        this.desc = desc;
    }

    public CodeNameItem(String name, String label, Object value, Object desc) {
        this.name = name;
        this.label = label;
        this.value = value;
        this.desc = desc;
    }

    /**
     * obj 转下拉框三个属性没有name
     * @param obj
     * @param nameProps
     * @param valueProps
     * @param descProps
     * @return
     */
    public static CodeNameItem fromObject(Object obj, String nameProps, String valueProps, String descProps) {
        try {
            String name = StringUtils.defaultString((String) PropertyUtils.getProperty(obj, nameProps));
            Object value = null;
            if (StringUtils.isNotBlank(valueProps)) {
                Object valueobj = PropertyUtils.getProperty(obj, valueProps);
                if (valueobj instanceof Long) {
                    value = String.valueOf(((Long) valueobj).longValue());
                }
            }
            Object desc = null;
            if (StringUtils.isNotBlank(descProps)) {
                desc = PropertyUtils.getProperty(obj, descProps);
            }
            return new CodeNameItem(name, value, desc);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /**
     * obj转下拉框4个属性
     * @param obj
     * @param nameProps
     * @param labelProps
     * @param valueProps
     * @param descProps
     * @return
     */
    public static CodeNameItem fromObject(Object obj, String nameProps, String labelProps, String valueProps, String descProps) {
        try {
            String name = StringUtils.defaultString((String) PropertyUtils.getProperty(obj, nameProps));
            String label = StringUtils.defaultString((String) PropertyUtils.getProperty(obj, labelProps));
            Object value = null;
            if (StringUtils.isNotBlank(valueProps)) {
                Object valueobj = PropertyUtils.getProperty(obj, valueProps);
                if (valueobj instanceof Long) {
                    value = String.valueOf(((Long) valueobj).longValue());
                }
            }
            Object desc = null;
            if (StringUtils.isNotBlank(descProps)) {
                desc = PropertyUtils.getProperty(obj, descProps);
            }
            return new CodeNameItem(name, label, value, desc);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }
}
